package com.moon.common.utils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("unchecked")
public class Page implements Serializable {

	private static final long serialVersionUID = 1L;
	private int start = 0;
	private int limit = 20;
	private List result = Collections.emptyList();
	private int totalCount = 0;
	private int pageCount = 0;
	private int pageNumber = 0;

	public Page() {
	}

	public Page(int start, int limit) {
		this.start = start;
		this.limit = limit;
	}

	public int getStart() {
		return this.start;
	}

	public void setStart(int start) {
		this.start = start;
		if (start < 0)
			this.start = 0;
	}

	public int getLimit() {
		return this.limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
		if (limit < 1) {
			this.limit = 1;
		}
		if (limit > 100)
			this.limit = 100;
	}

	public List getResult() {
		return this.result;
	}

	public void setResult(List result) {
		this.result = result;
	}

	public int getTotalCount() {
		return this.totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		this.pageCount = (totalCount % this.limit == 0 ? totalCount
				/ this.limit : totalCount / this.limit + 1);
		this.pageNumber = (this.start / this.limit + 1);
	}

	public int getPageCount() {
		return this.pageCount;
	}

	public int getPageNumber() {
		return this.pageNumber;
	}

	public boolean isHasNext() {
		return this.pageNumber + 1 <= this.pageCount;
	}

	public int getNextPage() {
		if (isHasNext()) {
			return this.pageNumber + 1;
		}
		return this.pageNumber;
	}

	public boolean isHasPre() {
		return this.pageNumber - 1 >= 1;
	}

	public int getPrePage() {
		if (isHasPre()) {
			return this.pageNumber - 1;
		}
		return this.pageNumber;
	}
}